//package com.zhongli.devplatform.controller.qq;
//
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.zhongli.devplatform.component.qq.QQClientConfig;
//import com.zhongli.devplatform.constant.Consts;
//import com.zhongli.devplatform.dto.UserBindDTO;
//import com.zhongli.devplatform.po.user.ReadUserPO;
//import com.zhongli.devplatform.security.qq.QQAccessToken;
//import com.zhongli.devplatform.security.qq.QQAuthenticationToken;
//import com.zhongli.devplatform.security.qq.QQUserInfo;
//import com.zhongli.devplatform.service.imp.qq.QQRegisterService;
//import com.zhongli.devplatform.service.imp.user.ReadUserService;
//import com.zhongli.devplatform.component.JwtTokenUtil;
//import com.zhongli.devplatform.component.SpringUtil;
//import com.zhongli.devplatform.component.qq.QQAuthUtil;
//import com.zhongli.devplatform.bean.Res;
//import io.swagger.annotations.ApiImplicitParam;
//import io.swagger.annotations.ApiImplicitParams;
//import io.swagger.annotations.ApiModelProperty;
//import io.swagger.annotations.ApiOperation;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.BadCredentialsException;
//import org.springframework.security.authentication.DisabledException;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.web.bind.annotation.*;
//
//import java.util.concurrent.TimeUnit;
//
///**
// * qq登录认证
// * @author WuXiCheng
// */
//@RestController
//@RequestMapping("/qq")
//@Slf4j
//public class QQAuthenticationController {
//
//    @Autowired
//    private AuthenticationManager authenticationManager;
//
//    @Autowired
//    private JwtTokenUtil jwtTokenUtil;
//
//    @Autowired
//    private UserDetailsService userDetailsService;
//
//    @Autowired
//    private ReadUserService userService;
//
//    @Autowired
//    private QQClientConfig clientConfig;
//
//    @Autowired
//    private QQAuthUtil authUtil;
//
//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;
//
//    @Autowired
//    private QQRegisterService qqRegisterService;
//
//    @Value("${token.expirationMilliSeconds}")
//    private long expirationMilliSeconds;
//
//    /**
//     * 通过code，qq登录认证
//     * @param code
//     * @return
//     * @throws Exception
//     */
//    @ApiModelProperty("QQ登录认证")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "query", name = "code", value = "用户换取access_token的code，仅在ErrCode为0时有效", required = true, dataType = "String"),
//            @ApiImplicitParam(paramType = "query", name = "role", value = "用户第三方登录需要选择的角色（student学生,parent老师）",required = true,dataType = "String")
//    })
//    @PostMapping("/login")
//    public Res qqLogin(@RequestParam("code") String code,@RequestParam("role") String role) throws Exception {
//        QQAccessToken qqAccessToken;
//        String openId = "";
//        try {
//            //拼接获取tokenUrl
//            String tokenAccessApi = String.format(clientConfig.getToken_access_api(),
//                    clientConfig.getAccessTokenUri(),
//                    clientConfig.getGrantType(),
//                    clientConfig.getAppId(),
//                    clientConfig.getSecret(), code,
//                    clientConfig.getRedirectUri());
//            //获取accessToken
//            qqAccessToken = authUtil.getToken(tokenAccessApi);
//            openId = authUtil.getOpenId(qqAccessToken.getAccessToken());
//            //获取qq用户信息
//            QQUserInfo qqUserInfo = authUtil.getUserInfo(qqAccessToken.getAccessToken(), openId);
//            qqUserInfo.setOpenId(openId);
//            QQRegisterService qqRegisterService = SpringUtil.getBean(QQRegisterService.class);
//            qqRegisterService.register(qqUserInfo,role);
//        } catch (Exception e) {
//            log.error("QQ登录失败【" + e.getMessage() + "】");
//        }
//        //认证已注册的qq用户
//        authenticate(openId);
//
//        String userName = userService.getOne(new QueryWrapper<ReadUserPO>().eq("qq_id", openId)).getAccountNum();
//        final UserDetails userDetails = userDetailsService
//                .loadUserByUsername(userName);
//        //封装redis集合
//        String key = Consts.USER_TOKEN + userDetails.getUsername();
//        //获取token
//        final String jwtToken = jwtTokenUtil.generateToken(userDetails);
//        //放入redis
//        stringRedisTemplate.opsForValue().set(key,jwtToken, expirationMilliSeconds, TimeUnit.MILLISECONDS);
//
//        return new Res<>(0,"登录成功",jwtToken);
//    }
//
//    /**
//     * QQ用户绑定
//     * @param userBindDTO
//     * @return
//     */
//    @ApiOperation(value = "QQ用户绑定")
//    @PostMapping("/bindQQUser")
//    private Res bindQQUser(@RequestBody UserBindDTO userBindDTO){
//        return qqRegisterService.bindQQUser(userBindDTO);
//    }
//
//
//
//    private void authenticate(String qqId) throws Exception {
//        try {
//            authenticationManager.authenticate(new QQAuthenticationToken(qqId));
//        } catch (DisabledException e) {
//            throw new Exception("USER_DISABLED", e);
//        } catch (BadCredentialsException e) {
//            throw new Exception("INVALID_CREDENTIALS", e);
//        }
//    }
//
//}
